#include<vector>
using namespace std;

struct ListNode {
      int val;
      ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
  };

class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        if(head == NULL || k <= 0)
            return NULL;

        int count = 0;
        ListNode *curr = head;
        while(curr != NULL){
            count++;
            curr = curr->next;
        }

        if(k>count)return NULL;

        int index = count - k;
        ListNode *c = head;
        while (index>0)
        {
            index--;
            c = c->next;

        }
        return c;
    }
};